
%macro prnt(program=, disname=, ds_list=, vis=, popfl=);

%get_data(lst=adex adsl,pre=,lib=adamw,supp=y);
%get_data(lst=da,pre=,lib=sdtmw,supp=y);

proc format;
value $trtfor
"Very Low Nicotine Cigarettes"="A"
"Usual Brand Cigarettes"="B"
"Nicorette Gum"="C"
;
run;

data adex1;
set adex;
by usubjid;
/*where avisitn in &vis and &popfl="Y";*/
where avisitn in (1 2 3) and SAFFL="Y";
trt=put(trta,trtfor.);
arm = "Study Product "||strip(trt);
column = trtan/10;
tocount=1;
/*if ASTDTM ne . and AENDTM ne . then dur=ASTDTM-AENDTM+1;*/
/*else dur=0;*/
if ASTDTM ne . and AENDTM ne . then dur=AENDTM-ASTDTM;
run;

*Count number of Product used in each tretament group by subject;

proc sql;
create table disp as select distinct subjid, sum(dur) as numbprod, trt, trtan from adex1 group by subjid, trt;
quit;

/*proc sql;*/
/*create table dispb as select distinct subjid, sum(dur) as numbprod, trt, trtan from adex1 where trt="B" group by subjid, trt;*/
/*quit;*/

proc sort data=adex1; by subjid trt; run;
proc sort data=disp; by subjid trt; run;

data adex2;
merge adex1 disp;
by subjid trt; 
visitnum=avisitn;
run;

/*data test; set adex2; if ASTDTM ne AENDTM then do; output; end; run;*/

proc sort data=da out=dar(keep=usubjid dascat visitnum dastresc dadtc datestcd darefid); 
by usubjid datestcd visitnum dadtc darefid dastresc; 
run;

data dar1;
set dar;
length exstdtc1 $16. darefidn 8.;
if datestcd="DISPAMT" then exstdtc1=dadtc;
	esle exstdtc1=" ";
darefidn=input(darefid,best.);
if index(dascat,"VERY LOW") then trt="A";
else if index(dascat,"USUAL BRAND") then trt="B";
else if index(dascat,"GUM") then trt="C";
run;

proc sort data=dar1; by usubjid datestcd visitnum dadtc dastresc; run;

data dar2; set dar1; by usubjid datestcd visitnum  dadtc dastresc; run;

data dard(rename=(dadtc=stdtc dastresc=dis) drop=datestcd) darr(rename=(dastresc=ret) drop=datestcd exstdtc1);
set dar2;
if datestcd="DISPAMT" then output dard;
if datestcd="RETAMT" then output darr;
run;

proc sort data=dard; by usubjid visitnum darefidn; run;
proc sort data=darr; by usubjid visitnum darefidn; run;

data daall(drop=dascat dis exstdtc1);
merge dard darr;
by usubjid visitnum darefidn;
ASTDTM=input(exstdtc1,??is8601dt.); format astdtm datetime18.;
run;

proc sql;
create table numpr as select distinct usubjid, count(ret) as prd, visitnum, trt from daall group by usubjid, visitnum;
quit;

proc sort data=numpr; by usubjid visitnum trt; run;
proc sort data=daall; by usubjid visitnum trt; run;

data datot;
merge daall numpr;
by usubjid visitnum trt; 
run;


/*data dad(keep=usubjid visitnum dast ASTDTM darefid) ;*/
/*set da;*/
/*if datestcd="DISPAMT" then dast=strip(dadtc); */
/*ASTDTM=input(dadtc,??is8601dt.); format astdtm datetime18.; */
/*if length(dast)<16 then delete;*/
/*if datestcd="DISPAMT" then output;*/
/*run;*/
/**/
/*data dar(keep=usubjid visitnum daen darefid daorres);*/
/*set da;*/
/*if datestcd="RETAMT" then do; daen=strip(dadtc); end; */
/*if length(daen)<16 then delete;*/
/*if datestcd="RETAMT" then output;*/
/*run;*/
/**/
/*proc sort data=dad; by usubjid visitnum darefid; run;*/
/*proc sort data=dar; by usubjid visitnum darefid; run;*/
/**/
/*data datot;*/
/*merge dad dar;*/
/*by usubjid visitnum darefid;*/
/*run;*/

proc sort data=adex2 out=adex2; by usubjid visitnum trt ASTDTM; run;
proc sort data=datot out=datot; by usubjid visitnum trt ASTDTM; run;
/*data adex3;*/
/*merge adex2 datot;*/
/*by usubjid visitnum trt ASTDTM; run;*/


proc sql;
create table adex3 as select dd.*, rr.stdtc, rr.dadtc, rr.ret, rr.darefidn, rr.prd from datot as rr
left join adex2 as dd on rr.usubjid=dd.usubjid and rr.visitnum=dd.visitnum and rr.ASTDTM=dd.ASTDTM and rr.trt=rr.trt
order by usubjid, visitnum, trt, ASTDTM;
quit;

proc sort data=adex3 nodupkey out=adex4; by usubjid visitnum trt ASTDTM dadtc darefidn; run;

/*proc sort data=adex3 nodupkey dupout=adex0; by usubjid visitnum trt ASTDTM dadtc darefidn; run;*/

proc sort data=adex4; by usubjid visitnum trt  ASTDTM; run;

data adex5; 
length totdur 8;
set adex4; 
dast1=input(stdtc,??is8601dt.); format dast1 datetime13.;
daen1=input(dadtc,??is8601dt.); format daen1 datetime13.;
/*if dast1 ne . and daen1 ne . then totdur=(daen1-dast1)/60; */
/*else totdur=1/60; */
if adur ne . then totdur=adur/60;
	else totdur=.;
format totdur best.;
if index(aform,"GUM") and dosea ne . then _dosea=1;
else _dosea=dosea;
run;

proc sql;
create table pru as select distinct sum(_dosea) as _dosea1, usubjid, visitnum, trt from adex5 where dosea ne . group by usubjid, visitnum, trt;
quit;

proc sort data=adex5; by usubjid visitnum trt; run;

data adex6;
merge adex5 pru;
by usubjid visitnum trt; 
run;

proc sort data=adex6 out=pop nodupkeys;
  by trt arm column;
run;

Proc sort data =pop out = spopone nodupkey ;
  by trt arm column;
run;

data tofmt;
   set spopone;
run;

proc sort data=adex6 out=temporary;
by trt usubjid;
run;

proc sql;
select count(distinct subjid) into :nba from temporary where trt="A";
select count(distinct subjid) into :nbb from temporary where trt="B";
select count(distinct subjid) into :nbc from temporary where trt="C";
quit;

%let nba=&nba; %let nbb=&nbb; %let nbc=&nbc;
%put &nba; %put &nbb; %put &nbc;

*******************************************************************************************************
Define specific formats for the report
******************************************************************************************************;
proc format;
 value level1l  1 = "Number of Product Used"
                2 = "Duration of Product Used (mins)"
;
run;

*******************************************************************************************************
Data manipulation
******************************************************************************************************;



%univariate6p(var=prd, width=5,decimal=0,out=_tmp1,level1=1,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,  /*wh=%str(_dosea ne .),*/
                   by=, in=temporary);

%univariate6p(var=totdur, width=5,decimal=0,out=_tmp2,level1=2,level1FMT=level1l,level2=,level2FMT=stat3_,level3=,   wh=%str(totdur ne .),
                   by=, in=temporary);

/*%univariate5(var=_dosea, */
/*                   width=5,*/
/*                   decimal=0,*/
/*                   out=_tmp1,*/
/*                   level1=1,*/
/*                   level2=,*/
/*                   level3=,*/
/*                   wh=%str(_dosea ne .),*/
/*                   by=,*/
/*                   in=temporary);*/
/**/
/*%univariate5(var=totdur,*/
/*                   width=5,*/
/*                   decimal=0,*/
/*                   out=_tmp2a,*/
/*                   level1=1,*/
/*                   level2=,*/
/*                   level3=,*/
/*                   wh=%str(totdur ne .),*/
/*                   by=,*/
/*                   in=temporary);*/
*******************************************************************************************************
Create report dataset
******************************************************************************************************;
data report;
set _tmp1 _tmp2; 
by level1 level2;
if LEVEL2_LABEL="N" then LEVEL2_LABEL="n";
	else LEVEL2_LABEL=LEVEL2_LABEL;
page=1;
run;

*Correct Number Duration n;
/*proc sql;*/
/*create table ndrt as select distinct sum(numbprod)/60 as ndr, trt as col, trtan, "Duration of Product Used (mins)" as level1_label length 200, 2 as level1, 1 as level2*/
/*	from disp group by trt;*/
/*quit;*/
/**/
/*proc sort data=ndrt; by level1 level2; run;*/
/**/
/*proc transpose data=ndrt out=ndrt1(drop=_name_);*/
/*by level1 level2;*/
/*id col;*/
/*var ndr;*/
/*run;*/
/**/
/*proc sort data=ndrt1; by level1 level2; run;*/
/**/
/*data report(drop=a b c);*/
/*merge report ndrt1;*/
/*by level1 level2;*/
/*if a ne . then do; col1=strip(put(a,best.)); end;*/
/*if b ne . then do; col2=strip(put(b,best.)); end;*/
/*if c ne . then do; col3=strip(put(c,best.)); end;*/
/*run;*/

/*%macro skip;*/
proc sql;
create table nnn as select count(distinct usubjid) as nnn, trt as col, 1 as level1, 1 as level2  from temporary group by trt;
quit;

proc sort data=nnn; by level1 level2; run;

proc transpose data=nnn out=nnnn(drop=_name_);
by level1 level2;
id col;
var nnn;
run;

proc sort data=nnnn; by level1 level2; run;

data report(drop=a b c);
merge report nnnn;
by level1 level2;
if a ne . then do; col1=strip(put(a,best.)); end;
if b ne . then do; col2=strip(put(b,best.)); end;
if c ne . then do; col3=strip(put(c,best.)); end;
run;
/*%mend skip;*/


proc sort data=report out=report;
 by level1 level2;
run;

proc sql;
 select count(distinct col1)
 into :nb_obse
 from report;
quit;

*******************************************************************************************************
Printing using proc report
******************************************************************************************************;
title3 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list.,program=&program.);

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&disname..PROD.USE.rtf";

%open_rtf;
%empty_ds_fix(report);

title4 "Summary of Product Use Part A";
title5 "(Safety Population) ";

/*footnote3 j=l "Note(s): N is by parameter either per subjects or per units.";*/
/*footnote4 j=l "         Estimated nicotine uptake = total cartridge weight change (mg) x nicotine concentration of the liquid (% by weight)";*/
/*footnote5 j=l " ";*/
/*footnote6 j=l " ";*/
/*footnote7 j=l " ";*/


proc report data=report nowd split='~' missing;
 column page level1 level2 level1_label  level2_label col1 col2 col3;
 define page         / " " order order=internal noprint;
 define level1       / " " order order=internal noprint;
 define level2       / "" order order=internal noprint;
 define level1_label / " " order order=internal style(column)=[cellwidth=2.9in just=left] style(header)=[just=left];
 define level2_label / " " order order=internal style(column)=[cellwidth=1.4in just=left] style(header)=[just=left];

 define col1 / " Study~Product A~(N=&nba)" style(column)=[cellwidth=1.6in just=c] style(header)=[just=c];
 define col2 / " Study~Product B~(N=&nbb)" style(column)=[cellwidth=1.6in just=c] style(header)=[just=c];
 define col3 / " Study~Product C~(N=&nbc)" style(column)=[cellwidth=1.6in just=c] style(header)=[just=c];

 break after page   / page;
 %if &nb_obse GT 0 %then %do;

 compute before level1 / style=[cellwidth=6in just=left];
  line " ";
 endcomp;
 %end;

 %if &nb_obse EQ 0 %then %do;
   compute after page/style=[cellwidth=9.7in just=left];
    line "No Values Were Measured.";
   endcomp;
 %end;

run;

ods rtf close;
ods listing;
%arrange_rtf;
%mend prnt;

%prnt(program=T14_01_04_01_PROD_USE, disname=14.1.4.1, ds_list=ADEX DA, vis=%str((1 2 3)), popfl=%str(SAFFL="Y"));

%clrw;
